const glob = require('glob');
const fs = require('fs');
// options is optional
glob('./src/style/theme/**/*.css', {}, function(er, files) {
   const themeObj = {};
  files.forEach((temp) => {
    const content = fs.readFileSync(temp, 'utf8');
    let themeClass = content.match(/\.theme-[\w\d]+\s+/)[0];
    let themeName = '';
    if (themeClass) {
      themeName = themeClass.replace(/(.*theme-|\s+)/g, '');
    }
    themeObj[themeName] = resolveVars(content);
  });
  fs.writeFileSync('./src/config/themeVars.json', JSON.stringify(themeObj, null, 2), 'utf8');
});

function resolveVars(content) {
  const matchs = content.match(/(--.*?):(.*?);/g);
  return matchs.reduce((obj, current) => {
    var splits = current.split(':');
    var key = (splits[0] + '').trim();
    var value = (splits[1].replace(/;/, '') + '').trim();
    obj[key] = value;
    return obj;
  }, {});
}
